﻿import game.GUI;
import game.Main;
import game.SandClock;
import game.Session;
import game.Word;
import game.WordDefinition;
import mx.utils.Delegate;
/**
* ...
* @author Default
*/

class game.Match {
	
	public var _originalWord:Word;
	public var _firstWord:Word;
	public var _secondWord:Word;
	public var _currentDefinition:WordDefinition;
	
	public function Match(definition:WordDefinition) {
		
		_currentDefinition = definition;
		Main.getInstance()._cGUI.loadBothImages(definition._sUrl, definition._sUrl);
	
		
		generateMatch();
	}
	
	public function goBack():Void {
		_originalWord.goBack();
	}
	
	public function haveWon():Boolean {
		var condition1:Boolean = (_firstWord.getWord() == _currentDefinition._sFirstWord) && (_secondWord.getWord() == _currentDefinition._sSecondWord);
		var condition2:Boolean = (_firstWord.getWord() == _currentDefinition._sSecondWord) && (_secondWord.getWord() == _currentDefinition._sFirstWord);
		
		return (condition1 || condition2);
	}
	
	private function generateMatch():Void {
		
		_originalWord = new Word(getMix(_currentDefinition._sFirstWord, _currentDefinition._sSecondWord), Main.getInstance()._cGUI.layerTiles );
		_firstWord = new Word("",  Main.getInstance()._cGUI.layerTiles )
		_secondWord = new Word("",  Main.getInstance()._cGUI.layerTiles )
		
			
		startRound();
	}
	
	public function startRound():Void {
	
			Main.getInstance()._cGUI.Theme = _currentDefinition._sTheme;
			Main.getInstance()._cGUI.showingTheme();
		
		SandClock.create( 30, Delegate.create( this, continueStartingRound ) );
	
	}
	
	public function continueStartingRound():Void {
			
			_originalWord.start();
	}
	
	private function getMix(sWord1:String, sWord2:String):String {
		var mix:String = "";
		var word1:String = sWord1;
		var word2:String = sWord2;
		
		while ( sWord1.length > 0 || sWord2.length > 0 ) { 
			var random:Number = Math.random();
			if ( sWord1.length > 0 && sWord2.length > 0) {
				if ( random > 0.5 ) {
					mix += sWord1.slice(0, 1);
					sWord1 = sWord1.substr(1, sWord1.length);
				} else {
					mix += sWord2.slice(0, 1);
					sWord2 = sWord2.substr(1, sWord2.length);
				}
			} else if (sWord1.length > 0) {
				mix += sWord1.slice(0, 1);
				sWord1 = sWord1.substr(1, sWord1.length);
								
			} else if (sWord2.length > 0) {
				mix += sWord2.slice(0,1);
				sWord2 = sWord2.substr(1, sWord2.length);
			}
		}
		
		
		if ( (mix == word1 + word2) || (mix == word2 + word1) ) {
			return getMix(word1, word2);
		} else {
			return mix;
		}
	}
	

	public function clear() {
		_originalWord.resetAll();
		_originalWord._nPreviousJumped = -1;
		_firstWord = new Word("",  Main.getInstance()._cGUI.layerTiles )
		_secondWord = new Word("",  Main.getInstance()._cGUI.layerTiles )
		
	}
	
	public function roundCompleteOk():Void {
		Main.getInstance()._cGUI.hidingTheme();
		GUI.getInstance().resetDoor();
		_originalWord.removeAll();
	}
	
}